package com.golddaymicromall.activity.salemanager;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.adapter.LogisticAdapter;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.LogisticInfo;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

public class LogisticActivity extends BaseActivity implements View.OnClickListener{

    //控件
    private LinearLayout mLogisticBack;
    private ListView mLogisticListView;
    private TextView mLogisticOrderId;
    private TextView mLogisticId;

    private String mOrderLogisticsid;
    private String orderId;
    private List<LogisticInfo> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistic);
        mOrderLogisticsid = this.getIntent().getStringExtra("orderLogisticsid");
        orderId = this.getIntent().getStringExtra("orderId");
        initView();
        attemptLogsticInfo(mOrderLogisticsid);
    }

    private void initView() {
        mLogisticBack = (LinearLayout) findViewById(R.id.logistic_back);
        mLogisticListView = (ListView) findViewById(R.id.logistic_listview);
        mLogisticOrderId = (TextView) findViewById(R.id.logistic_orderid);
        mLogisticId = (TextView) findViewById(R.id.logistic_id);
        mLogisticBack.setOnClickListener(this);
        mLogisticOrderId.setText(orderId);
        mLogisticId.setText(mOrderLogisticsid);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.logistic_back:
                finish();
                break;
            default:
                break;
        }
    }

    public void attemptLogsticInfo(String mOrderLogisticsid){
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.order.logisticsid");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("orderLogisticsid", mOrderLogisticsid);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(LogisticActivity.this, "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            Toast.makeText(LogisticActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            list = JSON.parseArray(result,LogisticInfo.class);
                            LogisticAdapter mLogisticAdapter = new LogisticAdapter(list,LogisticActivity.this);
                            mLogisticListView.setAdapter(mLogisticAdapter);
                        }
                    } else {
                        Toast.makeText(LogisticActivity.this, "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                },throwable -> {
                    showError();
                    throwable.printStackTrace();
                });

    }
}
